package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public Result findAll() {
        try {
            List<Role> roleList = roleDao.findAll();
            return new Result(true,"获取角色信息成功",roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取角色信息失败");
        }
    }

    @Override
    public PageResult selectAllByName(QueryPageBean queryPageBean) {
        //获取查询参数
        String queryString = queryPageBean.getQueryString();
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Role> rolePage = roleDao.selectAllByName(queryString);
        return new PageResult(rolePage.getTotal(),rolePage.getResult());
    }

    /*
     * 编辑角色信息
     * */
    @Override
    public Result update(Integer[] permissionIds, Role role,Integer[] menuIds) {
        try {
            //修改角色表
            roleDao.update(role);
            //修改对应权限表id  先删除 在添加
            roleDao.deletePermission(role.getId());
            for (Integer permissionId : permissionIds) {
                roleDao.add(permissionId,role.getId());
            }
            //修改对应菜单表id
            roleDao.deleteMenu(role.getId());
            for (Integer menuId : menuIds) {
                roleDao.addMenu(menuId,role.getId());
            }
            return new Result(true,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }

    /*
     * 角色添加
     * */
    @Override
    public Result add(Integer[] permissionIds, Role role,Integer[] menuIds) {
        try {
            //添加角色表
            roleDao.addRole(role);
            //添加该角色对应权限
            for (Integer permissionId : permissionIds) {
                roleDao.add(permissionId,role.getId());
            }
            //添加该角色对应菜单
            for (Integer menuId : menuIds) {
                roleDao.addMenu(menuId,role.getId());
            }
            return new Result(true,"添加角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加角色信息失败");
        }
    }

    @Override
    public Result delete(int id) {
        try {
            //查询是否管理其他表
            //管理权限
            Integer count = roleDao.findPermissionById(id);
            //查询用户
            Integer count1 = roleDao.findUserByid(id);
            //查询菜单
            Integer count2 = roleDao.findMenuByid(id);
            if (count!=0||count1!=0||count2!=0){
                //无法删除
                return new Result(false,"删除失败");
            }
            //删除
            roleDao.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"未知错误");
        }
    }
}
